﻿#include "FITKNastranBDFReader.h"
#include "FITKNastranAdaptorCase.h"
#include "FITKNastranBDFIOTool.h"

#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include <QFileInfo>

namespace Nastran
{
    FITKNastranBDFReader::FITKNastranBDFReader(bool &success)
        : _isSuccess(success)
    {
        _readerTool = new FITKNastranBDFIOReaderTool(this);
    }

    FITKNastranBDFReader::~FITKNastranBDFReader()
    {
        if (_readerTool) delete _readerTool;
    }

    // Entry point for the FITKNastranBDFReader class, executing the main process
    void FITKNastranBDFReader::run()
    {
        _isSuccess = this->reader();

        if (!_isSuccess)
            consoleMessage(3, "error adapting for case");
        else
            consoleMessage(1, QString("read BDF File succeed ! %1").arg(_fileName));

    }

    void FITKNastranBDFReader::consoleMessage(int level, const QString &str)
    {
        switch (level)
        {
        case 1:
            AppFrame::FITKMessageNormal(str);
            break;
        case 2:
            AppFrame::FITKMessageWarning(str);
            break;
        case 3:
            AppFrame::FITKMessageError(str);
            break;
        default:
            AppFrame::FITKMessageError(str);
            break;
        }
    }

    FITKNastranBDFIOReaderTool* FITKNastranBDFReader::getReaderTool()
    {
        return _readerTool;
    }

    QString FITKNastranBDFReader::readLine()
    {
        QString line;
        while (!atEnd())
        {
            //读取一行数据
            line = FITKAbstractTextReader::readLine();
            // 注释行跳过
            if (line.startsWith("$")) continue;
            break;
        }
        return line;
    }

    bool FITKNastranBDFReader::reader()
    {
        //获取算例数据
        QString fileName = QFileInfo(_fileName).fileName();
        AbaqusData::FITKDataCase* caseData = dynamic_cast<AbaqusData::FITKDataCase*>(_data);
        // Check if _caseData is null or if the file cannot be loaded, if so, exit the function
        if (nullptr == caseData || !(this->loadFile()) || !_readerTool)
            return false;
        caseData->setDataObjectName(fileName);
        //获取part管理器
        AbaqusData::FITKAbaqusPartManager *partManager = caseData->getPartManager();
        if (nullptr == partManager)
        {
            this->consoleMessage(2, "nullptr error for part manager");
            return false;
        }
        this->sendCurrentPercent(1);
        //创建新的part数据
        AbaqusData::FITKAbaqusPart* mesh = new AbaqusData::FITKAbaqusPart;
        mesh->setDataObjectName(partManager->checkName("Part-1"));
        partManager->appendDataObj(mesh);
        //设置工具数据
        _readerTool->setCaseData(caseData);
        _readerTool->setPartData(mesh);
        //创建读写适配器
        auto adaptor = FITKIOADAPTORFACTORY->createT<Nastran::FITKNastranAdaptorCase>("BDF", "NastranCase");
        // Check if the adaptor is null, if so, log an error message
        if (nullptr == adaptor)
        {
            consoleMessage(3, "create FITKNastranAdaptorCase Adaptor err!");
            return false;
        }
        adaptor->setReader(this);
        adaptor->setCaseData(caseData);
        adaptor->setPartData(mesh);
        // Attempt to adapt the data, if it fails, log an error message
        _isSuccess = adaptor->adaptR();
        this->sendCurrentPercent(99);
        _readerTool->updateData();
        this->sendCurrentPercent(100);
        // Delete the adaptor to free memory
        delete adaptor;
        return _isSuccess;
    }

} // namespace Nastran
